﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Pages_UserControls_ChangePassword : System.Web.UI.UserControl
{
    private Users user;
    private UsersBLL userBLL;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.userBLL = new UsersBLL();
        if (Session["User"] != null)
        {
            this.user = (Users)Session["User"];
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
    }

    protected void btnChange_Click(object sender, EventArgs e)
    {
        string oldPassword = this.txtOldPassword.Text;
        string newPassword = this.txtNewPassword.Text;
        string confirmPassword = this.txtConfirmPassword.Text;

        if (this.user.Password.Equals(oldPassword))
        {
            this.user.Password = newPassword;
            bool result = this.userBLL.GetIUsers.UpdateUser(user);
            if (result)
            {
                Response.Write("<script>alert('Đổi mật khẩu thành công')</script>");
                Response.Redirect("Login.aspx");
            }
            else
            {
                Response.Write("<script>alert('Đổi mật khẩu thất bại')</script>");
                Response.Redirect("Fail.aspx");
            }
        }
    }
}